package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.GzsdjlbBsDetail;
import com.ruoyi.report_ljlm.domain.vo.GzsdjlbBsDetailVo;
import com.ruoyi.report_ljlm.mapper.GzsdjlbBsDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.GzsdjlbBsInfoBo;
import com.ruoyi.report_ljlm.domain.vo.GzsdjlbBsInfoVo;
import com.ruoyi.report_ljlm.domain.GzsdjlbBsInfo;
import com.ruoyi.report_ljlm.mapper.GzsdjlbBsInfoMapper;
import com.ruoyi.report_ljlm.service.IGzsdjlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路面构造深度试验检测记录(手工铺砂法)Service业务层处理
 *
 * @author llb
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class GzsdjlbBsInfoServiceImpl implements IGzsdjlbBsInfoService {

    private final GzsdjlbBsInfoMapper baseMapper;
    private final GzsdjlbBsDetailMapper detailMapper;

    /**
     * 查询路面构造深度试验检测记录(手工铺砂法)
     */
    @Override
    @FIllBasicInfo
    public GzsdjlbBsInfoVo queryById(Long excelId) {
        GzsdjlbBsInfoVo gzsdjlbBsInfoVo = baseMapper.selectVoById(excelId);
        List<GzsdjlbBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<GzsdjlbBsDetail>().eq(GzsdjlbBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            gzsdjlbBsInfoVo.setDetailList(detailList);
        }
        return gzsdjlbBsInfoVo;
    }

    /**
     * 查询路面构造深度试验检测记录(手工铺砂法)列表
     */
    @Override
    public TableDataInfo<GzsdjlbBsInfoVo> queryPageList(GzsdjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GzsdjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<GzsdjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路面构造深度试验检测记录(手工铺砂法)列表
     */
    @Override
    public List<GzsdjlbBsInfoVo> queryList(GzsdjlbBsInfoBo bo) {
        LambdaQueryWrapper<GzsdjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GzsdjlbBsInfo> buildQueryWrapper(GzsdjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GzsdjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, GzsdjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, GzsdjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), GzsdjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), GzsdjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), GzsdjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), GzsdjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), GzsdjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), GzsdjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), GzsdjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), GzsdjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), GzsdjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), GzsdjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), GzsdjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), GzsdjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), GzsdjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), GzsdjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), GzsdjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), GzsdjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), GzsdjlbBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), GzsdjlbBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), GzsdjlbBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), GzsdjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), GzsdjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), GzsdjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), GzsdjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增路面构造深度试验检测记录(手工铺砂法)
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(GzsdjlbBsInfoBo bo) {
        GzsdjlbBsInfo add = BeanUtil.toBean(bo, GzsdjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<GzsdjlbBsDetail> details = new ArrayList<>(24);
            for (int i = 1; i <= 24; i++) {
                details.add(new GzsdjlbBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改路面构造深度试验检测记录(手工铺砂法)
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(GzsdjlbBsInfoBo bo) {
        GzsdjlbBsInfo update = BeanUtil.toBean(bo, GzsdjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), GzsdjlbBsDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GzsdjlbBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路面构造深度试验检测记录(手工铺砂法)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
